package com.ywj.boot_security.service.Impl;

import com.ywj.boot_security.entity.Role;
import com.ywj.boot_security.entity.User;
import com.ywj.boot_security.mapper.RoleMapper;
import com.ywj.boot_security.mapper.UserMapper;
import com.ywj.boot_security.service.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //  查询用户信息
        User user = userMapper.selUserByPhone(s);
        if(user != null){
            // 查询角色信息
            List<Role> role = roleMapper.findRolesByUserId(user.getId());
            // 设置用户角色信息（在实体类中）
            user.setRoleList(role);
            System.out.println(user.toString());
        }else {
            //不存在用户返回一个空的实体类，然后security会返回错误信息
            return new User();
        }
        return user;
    }
}

